//
//  LYLoginView.m
//  SZBank
//
//  Created by LuoYao on 16/7/8.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "LYLoginView.h"

@interface LYLoginView ()
{
    NSString *name,*password;/**< 登录用户名和密码 */
}
@property (nonatomic) UIImageView *loginImageView;/**< 登录窗口 */
@end
@implementation LYLoginView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self initUserInterFace];
        
    }
    return self;
}


/**< 初始化用户界面 */
- (void)initUserInterFace{
    
    NSArray *imageNames = @[@"登录_背景",@"上",@"左",@"右",@"登录_logo"];
    
    
    for (int i = 0; i < imageNames.count; i ++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        imageView.image = [LocalDataManager getImageWithSourceOfPath:imageNames[i]];
        imageView.userInteractionEnabled = YES;
        [self addSubview:imageView];
        
    }
    
       [self action_animations];
    
    
    //本地用户名密码读取
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"loginPerson"];
    name = dic[@"name"];
    password = dic[@"password"];
    
    //登录框
    self.loginImageView = [[UIImageView alloc] initWithFrame:CGRectMake(KWIDTH * 0.5, KHEIGHT * 0.3, KWIDTH * 0.4, KWIDTH * 0.15)];
    self.loginImageView.image = [LocalDataManager getImageWithSourceOfPath:@"登陆框"];
    self.loginImageView.userInteractionEnabled = YES;
    
    //userName
    UITextField *userName = [[UITextField alloc] initWithFrame:CGRectMake(self.loginImageView.bounds.size.width * 0.2, self.loginImageView.bounds.size.height * 0.12, self.loginImageView.bounds.size.width * 0.75, self.loginImageView.bounds.size.height * 0.2)];
    userName.text = dic[@"name"];
    
    userName.tag = 1002;
    [userName addTarget:self action:@selector(login_userNamePassword:) forControlEvents:UIControlEventEditingChanged];
    
    //userPassword
    UITextField *userPassword = [[UITextField alloc] initWithFrame:CGRectMake(self.loginImageView.bounds.size.width * 0.2, self.loginImageView.bounds.size.height * 0.45, self.loginImageView.bounds.size.width * 0.75, self.loginImageView.bounds.size.height * 0.2)];
    userPassword.secureTextEntry = YES;
    userPassword.text = dic[@"password"];
    
    [userPassword addTarget:self action:@selector(login_userNamePassword:) forControlEvents:UIControlEventEditingChanged];
    userPassword.tag = 1003;
    
    
    //loginBtn
    UIButton *loginBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.loginImageView.bounds.size.width * 0.7, self.loginImageView.bounds.size.height * 0.7, self.loginImageView.bounds.size.width * 0.3, self.loginImageView.bounds.size.height * 0.2)];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(action_removeLoginView) forControlEvents:UIControlEventTouchUpInside];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:30];
    
    
    //addView
    [self.loginImageView addSubview:userName];
    [self.loginImageView addSubview:userPassword];
    [self.loginImageView addSubview:loginBtn];
    
    [self.subviews.lastObject addSubview:self.loginImageView];
    
    

    
    
}

//获取登录填入的信息
- (void)login_userNamePassword:(UITextField *)sender{
    switch (sender.tag) {
        case 1002:
        {
            name = sender.text;
        }
            break;
        case 1003:
        {
            password = sender.text;
        }
        default:
            break;
    }
}

//登录按钮
- (void)action_removeLoginView{
    
    
    if (name.length == 0 || password.length == 0) {
        
        [LocalDataManager addAlertControllerWithControllerTitle:@"提示" controllerMessage:@"用户名和密码不能为空" actionSureTitle:@"确定" actionCancelTile:@"" actionSureBlock:nil actionCancelBlock:nil style:0];
        return;
    }
    
    NSDictionary *dic = @{@"username":name,@"pass":password};
    
    
    //登录动画
    [self.loginImageView removeFromSuperview];
    
    //load
    UIImageView *loadImageView = [[UIImageView alloc] initWithFrame:CGRectMake(KWIDTH * 0.65, KHEIGHT * 0.4, 200, 5)];
    loadImageView.image = [LocalDataManager getImageWithSourceOfPath:@"load"];
    [self addSubview:loadImageView];
    
    //遮罩
    for (int i = 0; i < 2; i ++) {
        UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(KWIDTH * 0.65 + i * 200 + i * KWIDTH * 0.1, KHEIGHT * 0.38, 50, 50)];
        lb.backgroundColor = [UIColor whiteColor];
        [self addSubview:lb];
    }
    [self action_load];
    
    //登录验证
    [NetWorkingDataManager getProtocolManagerLoginWithParam:dic success:^(id sender) {
        
        if ([sender[@"status"] isEqualToString:@"success"]) {
            
            //单例赋值
            [PersonInfoSingle defaultPersonInfo].loginPersonInfo = sender;
            
            //头像存入本地
            UIImage *headerImage = [LocalDataManager getImageWithURL:[PersonInfoSingle defaultPersonInfo].loginPersonInfo[@"HEADURL"]];
            
            NSData *imageData = UIImageJPEGRepresentation(headerImage, 1);
            
            [LocalDataManager saveImageWithData:imageData];
            
            
            //发送通知，登录成功。
            [[NSNotificationCenter defaultCenter] postNotificationName:@"loginsuccess" object:self];
            
            //本地记住密码
            NSDictionary *dic = @{@"name":name,@"password":password};
            [[NSUserDefaults standardUserDefaults] setObject:dic forKey:@"loginPerson"];
            
            [self removeSelfView];
        }else{
            [LocalDataManager addAlertControllerWithControllerTitle:@"登录失败" controllerMessage:sender[@"statusinfo"] actionSureTitle:@"确定" actionCancelTile:nil actionSureBlock:nil actionCancelBlock:nil style:0];
            [self addSubview:self.loginImageView];
        }
        
    } failure:^(id sender) {
        NSLog(@"失败：%@",sender);
        [LocalDataManager addAlertControllerWithControllerTitle:@"登录失败" controllerMessage:sender actionSureTitle:@"确定" actionCancelTile:nil actionSureBlock:nil actionCancelBlock:nil style:0];
    }];
    
}





- (void)action_animations{
    
    [UIView animateWithDuration:5.0 delay:0 options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction animations:^{
        
        [UIView setAnimationRepeatCount:INT_MAX];
    
        self.subviews[1].center = CGPointMake(KWIDTH * 0.5, KHEIGHT * 0.4);
        self.subviews[2].center = CGPointMake(KWIDTH * 0.6, KHEIGHT * 0.5);
        self.subviews[3].center = CGPointMake(KWIDTH * 0.4, KHEIGHT * 0.5);
        
        
    } completion:^(BOOL finished) {
        self.subviews[1].center = self.center;
        self.subviews[2].center = self.center;
        self.subviews[3].center = self.center;
    }];
}

//登录加载动画
- (void)action_load{
    [UIView animateWithDuration:2 animations:^{
        [UIView setAnimationRepeatCount:INT_MAX];
        self.subviews[5].center = CGPointMake(KWIDTH * 0.75, KHEIGHT * 0.4);
    } completion:^(BOOL finished) {
        [self addSubview:self.loginImageView];
    }];
}


//移除视图
- (void)removeSelfView{
    for (int i = 0; i < self.subviews.count; i ++) {
        if ([self.subviews[i] isKindOfClass:[UIImageView class]]) {
            ((UIImageView *)self.subviews[i]).image = nil;
        }
        
    }
    [self removeFromSuperview];
}

@end
